"""
系统角色枚举定义
统一维护系统角色的枚举、配置和元数据
"""
from enum import Enum
from typing import Dict, Any


class SystemRole(str, Enum):
    """系统角色枚举"""
    # System Scoped
    ADMIN = "admin"
    USER = "user"
    
    # Project Scoped
    OWNER = "owner"
    PROJECT_MANAGER = "project_manager"
    PRODUCT_MANAGER = "product_manager"
    OPERATIONS_SPECIALIST = "operations_specialist"
    OPS_ENGINEER = "ops_engineer"
    DEVELOPER = "developer"
    TESTER = "tester"
    VIEWER = "viewer"
    
    @classmethod
    def get_all_codes(cls) -> list[str]:
        """获取所有系统角色代码列表"""
        return [role.value for role in cls]


# 系统角色配置信息
# 注意：权限列表将从 ROLE_PERMISSIONS 自动生成，这里只定义基本信息
SYSTEM_ROLE_CONFIG: Dict[SystemRole, Dict[str, str]] = {
    SystemRole.ADMIN: {
        "name": "管理员",
        "code": "admin",
        "scope": "system",
        "description": "系统管理员，拥有所有权限",
    },
    SystemRole.USER: {
        "name": "普通用户",
        "code": "user",
        "scope": "system",
        "description": "普通用户，拥有基本访问权限",
    },
    SystemRole.OWNER: {
        "name": "项目拥有者",
        "code": "owner",
        "scope": "project",
        "description": "项目拥有者，拥有项目所有权限",
    },
    SystemRole.PROJECT_MANAGER: {
        "name": "项目经理",
        "code": "project_manager",
        "scope": "project",
        "description": "项目经理，负责项目管理和团队协调",
    },
    SystemRole.PRODUCT_MANAGER: {
        "name": "产品经理",
        "code": "product_manager",
        "scope": "project",
        "description": "产品经理，负责产品规划与需求分析",
    },
    SystemRole.OPERATIONS_SPECIALIST: {
        "name": "运营专员",
        "code": "operations_specialist",
        "scope": "project",
        "description": "运营专员，负责运营活动及数据分析",
    },
    SystemRole.OPS_ENGINEER: {
        "name": "运维工程师",
        "code": "ops_engineer",
        "scope": "project",
        "description": "运维工程师，负责环境维护与监控告警",
    },
    SystemRole.DEVELOPER: {
        "name": "开发者",
        "code": "developer",
        "scope": "project",
        "description": "开发人员，负责任务开发和版本管理",
    },
    SystemRole.TESTER: {
        "name": "测试人员",
        "code": "tester",
        "scope": "project",
        "description": "测试人员，负责测试用例管理和缺陷管理",
    },
    SystemRole.VIEWER: {
        "name": "查看者",
        "code": "viewer",
        "scope": "project",
        "description": "查看者，只有基础的查看权限",
    },
}


def get_system_role_config() -> Dict[SystemRole, Dict[str, str]]:
    """获取系统角色配置"""
    return SYSTEM_ROLE_CONFIG.copy()


def get_system_role_info(role: SystemRole) -> Dict[str, str]:
    """获取指定系统角色的信息"""
    return SYSTEM_ROLE_CONFIG.get(role, {}).copy()


def get_system_role_code(role: SystemRole) -> str:
    """获取系统角色代码"""
    return role.value


def get_system_role_name(role: SystemRole) -> str:
    """获取系统角色名称"""
    config = SYSTEM_ROLE_CONFIG.get(role, {})
    return config.get("name", role.value)


def is_system_role(role_code: str) -> bool:
    """检查是否为系统角色代码"""
    try:
        SystemRole(role_code)
        return True
    except ValueError:
        return False


def get_all_system_roles() -> list[SystemRole]:
    """获取所有系统角色枚举"""
    return list(SystemRole)

